// 路由自动注册
const requireDirectory = require('require-directory')
const Router = require('koa-router')

// 管理類
class InitMangaer {
    // 初始化
    static initCore(app) {

        InitMangaer.app = app
        InitMangaer.initLoadRouter()
        InitMangaer.loadHttpException();

    }

    // 路由自动注册
    static initLoadRouter() {
        const aipDirectory = `${process.cwd()}/app/api`
        requireDirectory(module, aipDirectory, {
            visit: whereLoadModule
        })

        function whereLoadModule(_router) {
            if (_router instanceof Router) {
                InitMangaer.app.use(_router.routes())
            }
        }
    }

    // 注册全局 异常类
    static loadHttpException (){
        const errors = require('./http-exception')
        global.errors = errors;
    }
}

module.exports = InitMangaer